<?php

namespace App\Http\Controllers\Admin\Examine;

use App\Exceptions\AppException;
use App\Http\Controllers\Controller;
use App\Http\Requests\examine\ConfigRequest;
use App\Models\AdminModel;
use App\Models\examine\ConfigModel;
use App\Models\StaffModel;
use Illuminate\Http\Request;

class Config extends Controller
{
    use ConfigRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $update_start = $request->input('update_start', '');
        $update_end = $request->input('update_end', '');

        $query = ConfigModel::query();


        if($update_start && $update_end){
            $query = $query->whereBetween('updated_at', [$update_start, $update_end]);
        }

        $items = $query->get();



        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'type' => $item->type,
                'type_name' => $item->type_name,
                'examine_type' => $item->examine_type,
                'examine_staff_id' => $item->examine_staff_id ? json_decode($item->examine_staff_id, true) : [],
                'copy_staff_id' => $item->copy_staff_id ? json_decode($item->copy_staff_id, true) : [],
                'updated_at' => $item->updated_at,
            ];
            return $res;
        });

        return $this->success(['list' => $list]);
    }


    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = ConfigModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        foreach ($params['examine_staff_id'] as $value){
            $admin_id = AdminModel::where('staff_id', $value['staff_id'])->value('id');
            if(!$admin_id){
                return $this->error('审批员工需要对应的管理后台账号');
            }
        }

        $model->examine_type = $params['examine_type'];
        $model->examine_staff_id = json_encode($params['examine_staff_id']);
        $model->copy_staff_id = json_encode($params['copy_staff_id']);
        $model->updated_at = date('Y-m-d H:i:s');
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = ConfigModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

    /**
     * Notes:流程员工
     * User: xiangzi
     * Date: 2024/9/14
     * Time: 11:35
     * @return array
     */
    public function examine_staff($id)
    {
        $model = ConfigModel::find($id);
        $examine_staff_id = json_decode($model->examine_staff_id, true);

        $return = [];
        foreach ($examine_staff_id as $staff_id) {
            $staff_info = StaffModel::with('role')->find($staff_id);
            $return[] = [
                'staff_name' => $staff_info->name,
                'staff_role' => $staff_info->role ? $staff_info->role->name : ''
            ];
        }

        return $return;
    }


    /**
     * Notes:审批流程
     * User: xiangzi
     * Date: 2024/9/27
     * Time: 18:00
     * @return \Illuminate\Http\JsonResponse
     */
    public function examine_process(Request $request)
    {
        $params = $request->all();

        $check_res = self::examine_process_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }
        $config = ConfigModel::where('type', $params['type'])->first();

        $return['examine_type'] = $config->examine_type;
        $return['examine_type_name'] = ConfigModel::EXAMINE_TYPE[$config->examine_type] ?? '';

        if($config->examine_staff_id){
            $examine_staff_id = json_decode($config->examine_staff_id, true);

            foreach ($examine_staff_id as $value) {
                $staff_info = StaffModel::with('role')->find($value['staff_id']);
                if($staff_info){
                    $return['examine_list'][] = [
                        'sort' => $value['sort'],
                        'staff_id' => $staff_info->id,
                        'staff_name' => $staff_info->name ?? '',
                        'staff_role' => $staff_info->role ? $staff_info->role->name : '',
                    ];
                }
            }
        }else{
            $return['examine_list'] = [];
        }

        if($config->copy_staff_id){
            $copy_staff_id = json_decode($config->copy_staff_id, true);

            foreach ($copy_staff_id as $staff_id) {
                $staff_info = StaffModel::with('role')->find($staff_id);
                if($staff_info){
                    $return['copy_list'][] = [
                        'staff_id' => $staff_info->id,
                        'staff_name' => $staff_info->name ?? '',
                        'staff_role' => $staff_info->role ? $staff_info->role->name : '',
                    ];
                }
            }
        }else{
            $return['copy_list'] = [];
        }

        return $this->success($return);
    }

}
